This page is about trend in personal consumption in the US

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.5     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.0.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rvest)
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:readr':
## 
##     guess_encoding
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

Import data

consumption_product = readxl::read_excel("data/consumption_product.xlsx") %>% 
  janitor::clean_names() %>% 
  pivot_longer(
    x2019_q1 : x2021_q3,
    names_to = "time",
    names_prefix = "x",
    values_to = "consumption"
  ) 

consumption_function = readxl::read_excel("./data/consumption_function.xlsx") %>% 
  janitor::clean_names() %>% 
  filter(as.numeric(line) <= 28) %>% 
    pivot_longer(
    x2019_q1 : x2021_q3,
    names_to = "time",
    names_prefix = "x",
    values_to = "consumption"
  ) %>% 
  mutate(functions = recode(functions, `Household consumption expenditures (for services)` = "household",
                           `Final consumption expenditures of nonprofit institutions serving households (NPISHs)1` = "nonprofit consumption"))

general_1 = consumption_function %>% 
  filter(functions %in% c("Goods","Services"))

fig_1 = plot_ly(general_1, x = ~time, y = ~consumption, type = "scatter", mode = "lines", color = ~functions)

1st level

general_1 = consumption_function %>% 
  filter(functions %in% c("Goods","Services"))

fig_1 = plot_ly(general_1, x = ~time, y = ~consumption, type = "scatter", mode = "lines", color = ~functions)

fig_1
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

2nd level

general_2 = consumption_function %>% 
  filter(functions %in% c("Durable goods","Nondurable goods","household","nonprofit consumption")) %>% 
  select(-line) %>% 
  pivot_wider(names_from = functions, values_from = consumption) %>% 
  janitor::clean_names()

subfig_1 = plot_ly(general_2, x = ~time, y = ~durable_goods, type = "bar", name = "Durable Goods") %>% 
  add_trace(y = ~nondurable_goods, name = "Nondurable Goods") %>% 
  layout(yaxis = list(title = "Consumption"), barmode = "stack")

subfig_2 = plot_ly(general_2, x = ~time, y = ~household, type = "bar", name = "Household") %>% 
  add_trace(y = ~nonprofit_consumption, name = "Nonprofit Consumption") %>% 
  layout(yaixs = list(title = "Consumption"), barmode = "stack")

subfig_1
subfig_2
## Warning: 'layout' objects don't have these attributes: 'yaixs'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'

3rd level

# durable goods
consumption_function %>% 
  filter(functions %in% c("Motor vehicles and parts","Furnishings and durable household equipment","Recreational goods and vehicles","Other durable goods")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h'))
# nondurable goods
consumption_function %>% 
  filter(functions %in% c("Food and beverages purchased for off-premises consumption","Clothing and footwear","Gasoline and other energy goods","Other nondurable goods")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h'))
# household consumption
consumption_function %>% 
  filter(functions %in% c("Housing and utilities","Health care","Transportation services","Recreation services","Food services and accommodations","Financial services and insurance","Other services")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h'))
# nonprofit serving household
consumption_function %>% 
  filter(functions %in% c("Gross output of nonprofit institutions2","Less: Receipts from sales of goods and services by nonprofit institutions3")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h'))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels